import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:video_player/video_player.dart';

class PreviewVideoPage extends StatefulWidget {
  String filePath;

  PreviewVideoPage({Key? key, required this.filePath}) : super(key: key);

  @override
  _PreviewVideoPageState createState() => _PreviewVideoPageState();
}

class _PreviewVideoPageState extends State<PreviewVideoPage> {
  late VideoPlayerController _controller;
  late Future<void> _initializeVideoPlayerFuture;

  @override
  void initState() {
    super.initState();

    // Create and store the VideoPlayerController. The VideoPlayerController
    // offers several different constructors to play videos from assets, files,
    // or the internet.
    _controller = VideoPlayerController.file(File(widget.filePath));

    // Initialize the controller and store the Future for later use.
    _initializeVideoPlayerFuture = _controller.initialize();

    // Use the controller to loop the video.
    _controller.setLooping(true);
  }

  @override
  void dispose() {
    // Ensure disposing of the VideoPlayerController to free up resources.
    _controller.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // Use a FutureBuilder to display a loading spinner while waiting for the
      // VideoPlayerController to finish initializing.
      body: FutureBuilder(
        future: _initializeVideoPlayerFuture,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            // If the VideoPlayerController has finished initialization, use
            // the data it provides to limit the aspect ratio of the video.

            return Container(
                height: double.infinity,
                color: Colors.black87,
                child: Stack(
                  children: [
                    GestureDetector(
                      onTap: () {
                        _startAndStop();
                      },
                      child: Stack(
                        alignment: AlignmentDirectional.center,
                        children: [
                          AspectRatio(
                              aspectRatio: _controller.value.aspectRatio,
                              // Use the VideoPlayer widget to display the video.
                              child: VideoPlayer(_controller)),
                          Visibility(
                            visible: !_controller.value.isPlaying,
                            child: Image.asset(
                              "assets/images/album_video_play.webp",
                              width: 60,
                              height: 60,
                            ),
                          )
                        ],
                      ),
                    ),
                    Positioned(
                      bottom: 16,
                      left: 16,
                      child: GestureDetector(
                        child: Image.asset(
                          "assets/images/camera_photo_back.webp",
                          width: 96,
                          height: 96,
                        ),
                        onTap: () {
                          Get.back();
                        },
                      ),
                    ),
                    Positioned(
                      bottom: 16,
                      right: 16,
                      child: GestureDetector(
                        child: Image.asset(
                          "assets/images/camera_photo_done.webp",
                          width: 96,
                          height: 96,
                        ),
                        onTap: () {
                          Get.back(result: "done");
                        },
                      ),
                    )
                  ],
                ));
          } else {
            // If the VideoPlayerController is still initializing, show a
            // loading spinner.
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }

  void _startAndStop() {
    setState(() {
      // If the video is playing, pause it.
      if (_controller.value.isPlaying) {
        _controller.pause();
      } else {
        // If the video is paused, play it.
        _controller.play();
      }
    });
  }
}
